/*
 * HistoryFrame.java
 *
 * Created on 28 listopad 2007, 17:11
 * This Frame uses JCalendar created by Kai Toedter
 */

package pl.edu.pw.mini.gui;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import pl.edu.pw.mini.history.*;

/**
 * Frame that allows user to see all history entries, search history entries, change settings
 * @author Diana
 */
public class HistoryFrame extends javax.swing.JFrame {
    
    
    /**
     * form of displaying date
     */
    protected SimpleDateFormat dateForm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    /** Creates new form HistoryFrame */
    public HistoryFrame() {
        initComponents();
        History history = HistoryManager.getHistoryEntries();
        HistoryManager.updateHistory(history);
        history = HistoryManager.getHistoryEntries();
        setHistoryListIntoTable(history);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        btnClearHistory = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btnHistoryLength = new javax.swing.JComboBox();
        btnSetDurability = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblHistory = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btnSearch = new javax.swing.JButton();
        chbxName = new javax.swing.JCheckBox();
        txtPhraseToCheck = new javax.swing.JTextField();
        chbxAction = new javax.swing.JCheckBox();
        cbxAction = new javax.swing.JComboBox();
        chbxDate = new javax.swing.JCheckBox();
        rbtnBefore = new javax.swing.JRadioButton();
        rbtnAfter = new javax.swing.JRadioButton();
        rbtnOn = new javax.swing.JRadioButton();
        dtchDate = new com.toedter.calendar.JDateChooser();
        btnOK = new javax.swing.JButton();
        btnShowAllHistory = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("History");
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                ComponentShownHandler(evt);
            }
        });

        jLabel1.setText("Search:");

        btnClearHistory.setText("Clear history");
        btnClearHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearHistoryActionPerformed(evt);
            }
        });

        jLabel2.setText("Delete history older than");

        btnHistoryLength.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1 day", "2 days", "3 days", "1 week", "1 month" }));
        btnHistoryLength.setSelectedIndex(3);

        btnSetDurability.setText("Set");
        btnSetDurability.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetDurabilityActionPerformed(evt);
            }
        });

        jLabel4.setText("History:");

        tblHistory.setAutoCreateRowSorter(true);
        tblHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Document's name", "Date", "Action"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblHistory);

        btnSearch.setText("OK");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        chbxName.setSelected(true);
        chbxName.setText("in documents' names:");
        chbxName.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chbxName.setMargin(new java.awt.Insets(0, 0, 0, 0));

        chbxAction.setText("by action:");
        chbxAction.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chbxAction.setMargin(new java.awt.Insets(0, 0, 0, 0));

        cbxAction.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Download from Zoho", "Upload to Zoho", "Upload to Google" }));

        chbxDate.setText("by date:");
        chbxDate.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        chbxDate.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(rbtnBefore);
        rbtnBefore.setText("before");
        rbtnBefore.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtnBefore.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(rbtnAfter);
        rbtnAfter.setText("after");
        rbtnAfter.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtnAfter.setMargin(new java.awt.Insets(0, 0, 0, 0));

        buttonGroup1.add(rbtnOn);
        rbtnOn.setText("on");
        rbtnOn.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        rbtnOn.setMargin(new java.awt.Insets(0, 0, 0, 0));

        btnOK.setText("Close");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cbxAction, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtPhraseToCheck, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addGap(58, 58, 58)
                                .addComponent(dtchDate, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(chbxAction))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(chbxName))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(chbxDate)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rbtnOn)
                                    .addComponent(rbtnBefore)
                                    .addComponent(rbtnAfter)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))))
                        .addGap(11, 11, 11)))
                .addContainerGap(312, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(348, Short.MAX_VALUE)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(70, 70, 70))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(chbxName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtPhraseToCheck, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chbxAction)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbxAction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(chbxDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnBefore)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(rbtnOn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rbtnAfter))
                    .addComponent(dtchDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addComponent(btnSearch)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(btnOK)
                .addContainerGap())
        );

        btnShowAllHistory.setText("View all history");
        btnShowAllHistory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllHistoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 813, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnClearHistory)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnShowAllHistory))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(159, 159, 159)
                                    .addComponent(btnSetDurability))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(btnHistoryLength, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(55, 55, 55)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(89, 89, 89)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClearHistory)
                    .addComponent(btnShowAllHistory))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(14, 14, 14)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(14, 14, 14)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnHistoryLength, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnSetDurability)))))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        // wyszukiwanie:
        History history = HistoryManager.getHistoryEntries();
        int i = 0;
        if(chbxName.isSelected()){
            for(i=0; i<history.historyEntries.size(); i++){
                if(!history.historyEntries.get(i).getValue().contains(txtPhraseToCheck.getText())){
                    history.historyEntries.remove(i);
                    i--;
                }
            }
        }
        if(chbxAction.isSelected()){
            for(i=0; i<history.historyEntries.size(); i++){
                if(!((cbxAction.getSelectedIndex() == 0 && history.historyEntries.get(i).getType() == HistoryEntry.TypeOfEntry.DownloadZoho) || (cbxAction.getSelectedIndex() == 1 && history.historyEntries.get(i).getType() == HistoryEntry.TypeOfEntry.UploadZoho) || (cbxAction.getSelectedIndex() == 2 && history.historyEntries.get(i).getType() == HistoryEntry.TypeOfEntry.UploadGoogle))){
                    history.historyEntries.remove(i);
                    i--;
                }
            }
        }
        if(chbxDate.isSelected()){
            if(dtchDate.getDate()!=null){
                Date date;
                Calendar cal = new GregorianCalendar();
                cal.setTime(dtchDate.getDate());
                cal.set(Calendar.HOUR, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                date = new Date(cal.getTimeInMillis());
                cal.add(Calendar.DAY_OF_MONTH, 1);
                Date date2 = new Date(cal.getTimeInMillis());
                for(i=0; i<history.historyEntries.size(); i++){
                    if(rbtnBefore.isSelected()){
                        if(!history.historyEntries.get(i).getDate().before(date)){
                            history.historyEntries.remove(i);
                            i--;
                        }
                    } else if(rbtnOn.isSelected()){
                        if(!(history.historyEntries.get(i).getDate().after(date) && (history.historyEntries.get(i).getDate().before(date2)))){
                            history.historyEntries.remove(i);
                            i--;
                        }
                    } else if(rbtnAfter.isSelected()){
                        if(!history.historyEntries.get(i).getDate().after(date2)){
                            history.historyEntries.remove(i);
                            i--;
                        }
                    }
                }
            }
        }
        setHistoryListIntoTable(history);
    }//GEN-LAST:event_btnSearchActionPerformed
    
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        setVisible(false);
    }//GEN-LAST:event_btnOKActionPerformed
    
    private void setHistoryListIntoTable(History history){
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("Document's title");
        model.addColumn("Date");
        model.addColumn("Action");
        int i;
        for(i=0; i<history.historyEntries.size(); i++){
            model.addRow(new String[] {history.historyEntries.get(i).getValue(), dateForm.format(history.historyEntries.get(i).getDate()), history.historyEntries.get(i).typeToString()});
        }
        tblHistory.setModel(model);
    }
    private void btnShowAllHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllHistoryActionPerformed
        History history = HistoryManager.getHistoryEntries();
        setHistoryListIntoTable(history);
    }//GEN-LAST:event_btnShowAllHistoryActionPerformed
    
    private void btnClearHistoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearHistoryActionPerformed
        History history = HistoryManager.getHistoryEntries();
        HistoryManager.clearHistory(history.durability);
        history = HistoryManager.getHistoryEntries();
        setHistoryListIntoTable(history);
        setProperDurability(history);
    }//GEN-LAST:event_btnClearHistoryActionPerformed
    
    private int getChosenDurability(){
        switch(btnHistoryLength.getSelectedIndex()){
            case 0:
                return 1;
            case 1:
                return 2;
            case 2:
                return 3;
            case 3:
                return 7;
            default:
                return 30;
        }
    }
    
    private void btnSetDurabilityActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetDurabilityActionPerformed
        HistoryManager.changeDurability(getChosenDurability());
        History history = HistoryManager.getHistoryEntries();
        setProperDurability(history);
        HistoryManager.updateHistory(history);
        setHistoryListIntoTable(HistoryManager.getHistoryEntries());
    }//GEN-LAST:event_btnSetDurabilityActionPerformed
    
    private void setProperDurability(History history){
        switch(history.durability){
            case 1:
                btnHistoryLength.setSelectedIndex(0);
                break;
            case 2:
                btnHistoryLength.setSelectedIndex(1);
                break;
            case 3:
                btnHistoryLength.setSelectedIndex(2);
                break;
            case 7:
                btnHistoryLength.setSelectedIndex(3);
                break;
            default:
                btnHistoryLength.setSelectedIndex(4);
                break;
        }
    }
    
    private void ComponentShownHandler(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_ComponentShownHandler
        History history;
        history = HistoryManager.getHistoryEntries();
        //SetModelIntoListHistory(history);
        setHistoryListIntoTable(history);
        setProperDurability(history);
    }//GEN-LAST:event_ComponentShownHandler
    
    /**
     * main
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HistoryFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClearHistory;
    private javax.swing.JComboBox btnHistoryLength;
    private javax.swing.JButton btnOK;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnSetDurability;
    private javax.swing.JButton btnShowAllHistory;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JComboBox cbxAction;
    private javax.swing.JCheckBox chbxAction;
    private javax.swing.JCheckBox chbxDate;
    private javax.swing.JCheckBox chbxName;
    private com.toedter.calendar.JDateChooser dtchDate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JRadioButton rbtnAfter;
    private javax.swing.JRadioButton rbtnBefore;
    private javax.swing.JRadioButton rbtnOn;
    private javax.swing.JTable tblHistory;
    private javax.swing.JTextField txtPhraseToCheck;
    // End of variables declaration//GEN-END:variables
    
}
